package com.gameLogic.utils
{
	public class ObjDictionary
	{
		
		private var _strongType:Class;     //要求存储对象为强类型
		
		private var _container:Object;
		public function get container():Object
		{
			return _container;
		}
		
		private var _length:uint;
		public function get length():uint
		{
			return _length;
		}
		
		public function ObjDictionary(strongType:Class = null)
		{
			_strongType = strongType;
			_container = new Object();
		}
		
		/**
		 * 添加元素 
		 * @param key
		 * @param value
		 * 
		 */		
		public function add(key:*, value:*):void 
		{
			if(_strongType && !(value is _strongType))
			{
				throw new Error("Obj Dictionary [add] Type Check Error, Need" + _strongType);
				//return;
			}
			//如果是新添加才增加length
			if(!containsKey(key))
				_length++;
			_container[key] = value;
		}
		
		/**
		 * 根据键值获取对象 
		 * @param key
		 * @return 
		 * 
		 */		
		public function get(key:*):*
		{
			return _container[key];
		}
		
		/**
		 * 重新设置 
		 * @param key
		 * @param value
		 * 
		 */		
		public function reset(key:*, value:*):void
		{
			if(_strongType && !(value is _strongType))
			{
				throw new Error("Obj Dictionary [add] Type Check Error, Need" + _strongType);
			}
			if(containsKey(key))
			{
				_container[key] = value;
			}
			else
			{
				trace("ObjDictionary: warning you reset a not exist key");
			}
		}
		
		/**
		 * 是否包含键 
		 * @param key
		 * @return 
		 * 
		 */		
		public function containsKey(key:*):Boolean
		{
			return _container.hasOwnProperty(key);
		}
		
		/**
		 * 移除键 
		 * @param key
		 * 
		 */		
		public function remove(key:*):void
		{
			if(_container.hasOwnProperty(key))
			{
				_container[key] = null;
				delete _container[key];
				_length--;
			}
		}
		
		/**
		 *清除操作 
		 * 
		 */		
		public function clear():void
		{
			_length = 0;
			_container = null;
			_container = new Object();
		}
		
	}
}